package be.xios.projectquiz.database;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import net.miginfocom.swing.MigLayout;

/// <summary>
/// Deze klasse is de GUI om een restore te doen
/// </summary>
/// <author>Flemming Bosch</author>
public class RestoreGUI extends JFrame{
	private static final long serialVersionUID = 1L;

	private JPanel getData;
	private JPanel restore;
	
		private JLabel usernameLbl;
		private JLabel passwordLbl;
		private JLabel dbUrlLbl;
		private JLabel dbPortLbl;
		private JLabel dbNameLbl;
		private JLabel dbFileLbl;
		
		private JButton chooseBtn;
		private JButton restoreBtn;
		
		private JTextField usernameTxt;
		private JTextField passwordTxt;
		private JTextField dbUrlTxt;
		private JTextField dbPortTxt;
		private JTextField dbNameTxt;
		private JTextField dbFileTxt;
		private Restore r = new Restore();
	
	/// <summary>
	/// Dit is de default constructor waar de GUI word in aangemaakt.
	/// </summary>
	/// <author>Flemming Bosch</author>
	public RestoreGUI(){
		super("Restore Tool - MySQL Databases");
			
			//TopPanel 
			getData = new JPanel(new MigLayout());
			getData.setBorder(new TitledBorder("Retrieve Data"));
			
				//Labels aanmaken
				usernameLbl = new JLabel("Username ");
				passwordLbl = new JLabel("Password ");
				dbUrlLbl = new JLabel("Database Url/Host ");
				dbPortLbl = new JLabel("Database Port ");
				dbNameLbl = new JLabel("Database Name ");
				dbFileLbl = new JLabel("Database .SQL File ");
				
				//Buttons aanmaken
				chooseBtn = new JButton("Choose .SQL File");
				chooseBtn.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						dbFileTxt.setText(getFilePath());
					}
				});
				
				restoreBtn = new JButton("Restore Database");
				
				//Textfields aanmaken
				usernameTxt = new JTextField(15);
				passwordTxt = new JTextField(15);
				dbUrlTxt = new JTextField(15);
				dbPortTxt = new JTextField("3306",5);
				dbNameTxt = new JTextField(15);
				dbFileTxt = new JTextField(15);
				
			
			//Labels,Textfields en Buttons toevoegen op TopPanel
			getData.add(usernameLbl);
			getData.add(usernameTxt,"wrap");
			getData.add(passwordLbl);
			getData.add(passwordTxt,"wrap");
			getData.add(dbUrlLbl);
			getData.add(dbUrlTxt,"wrap");
			getData.add(dbPortLbl);
			getData.add(dbPortTxt,"wrap");
			getData.add(dbNameLbl);
			getData.add(dbNameTxt,"wrap");
			getData.add(dbFileLbl);
			getData.add(dbFileTxt);
			getData.add(chooseBtn,"wrap");
				
			//BottomPanel
			restore = new JPanel(new FlowLayout(1));
		
			//Buttons aanmaken
			restoreBtn = new JButton("Restore Database");
			restoreBtn.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					if(e.getSource().equals(restoreBtn)){
						//ingevulde gegevens naar een variabele schrijven
						String username = usernameTxt.getText();
						String pw = passwordTxt.getText();
						String host = dbUrlTxt.getText();
						String port = dbPortTxt.getText();
						String filepath = dbFileTxt.getText();
						String dbname = dbNameTxt.getText();
					
						//restore uitvoeren met de verkregen gegevens
						r.restoreDB(host, port, username, pw, dbname, filepath);
					
						//na het uivoeren, textfields clearen!
						usernameTxt.setText("");
						passwordTxt.setText("");
						dbUrlTxt.setText("");
						dbPortTxt.setText("3306");
						dbFileTxt.setText("");
						dbNameTxt.setText("");
					}
				}
			});
			
			//Button toevoegen aan Panel
			restore.add(restoreBtn);
		
		//TopPanel & BottomPanel toevoegen aan Frame
		add(getData,BorderLayout.CENTER);
		add(restore,BorderLayout.SOUTH);
	}
	
	/// <summary>
	/// Deze method verkrijgt het restore path waar de .sql file vandaan moet gehaald worden.
	/// </summary>
	/// <author>Flemming Bosch</author>
	public static String getFilePath() {
		String FilePath = "";
	    JFileChooser fc = null;

	    if (fc == null) {
	    	fc = new JFileChooser();
	        FileNameExtensionFilter filter = new FileNameExtensionFilter("SQL File (.sql)", "sql");  
	        fc.setFileFilter(filter);
	        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
	        fc.setAcceptAllFileFilterUsed(false);
	    }
	    
	    int returnVal = fc.showDialog(null, "Kies je database (.sql) om te restoren.");
	    if (returnVal == JFileChooser.APPROVE_OPTION) {
	    	File file = fc.getSelectedFile();
	        FilePath = file.getAbsolutePath();
	    }
	    
	    //FilePath als een string returnen
	    return FilePath;
	}
}
